<template>
  <div class="vendor-connect-person">
    <h3>联系人</h3>
    <el-form :model="connectInfo" :rules="rules" ref="connectInfo" label-width="100px">
      <el-form-item label="联系人" prop="contactName">
        <el-input v-model="connectInfo.contactName"></el-input>
      </el-form-item>
      <el-form-item label="联系方式" prop="contact">
        <el-input v-model="connectInfo.contact"></el-input>
      </el-form-item>
      <el-form-item label="电话" prop="phone">
        <el-input v-model="connectInfo.phone"></el-input>
      </el-form-item>
      <el-form-item label="传真" prop="fax">
        <el-input v-model="connectInfo.fax"></el-input>
      </el-form-item>
      <el-form-item label="email" prop="email">
        <el-input v-model="connectInfo.email"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="updateConnectForm('connectInfo')">更新</el-button>
        <el-button @click="resetConnectForm('connectInfo')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script type="text/ecmascript-6">
  import axios from 'axios'
  export default{
    data () {
      return {
        connectInfo: {},
        rules: {
          email: [
            {type: 'email', required: false, message: '请输入正确的邮箱地址！'}
          ],
          contactName: [
            {required: true, message: '20个字符以内'}
          ],
          phone: [
            {required: true, message: '请输入正确手机号码', validator: this.phoneNumberCheck}
          ]
        },
        vendorId: this.$route.params.id
      }
    },
    methods: {
      resetConnectForm (formName) {
        this.$refs[formName].resetFields()
      },
      phoneNumberCheck (rule, value, callback) {
        if (value === '') {
          callback(new Error('请输入电话号码'))
        } else if (!(/^(0|86|17951)?(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$/.test(value))) {
          callback(new Error('请输入正确的格式!'))
        } else {
          callback()
        }
      },
      updateConnectForm (formName) {
        this.$refs[formName].validate((valid) => {
          let param = this.connectInfo
          param.id = this.vendorId
          if (valid) {
            axios.post('/api/vendor/updateVendor', param).then(res => {
              if (res.data.code === 9999) {
                this.$notify({
                  title: '成功',
                  message: '即将跳转到商家详情页。',
                  type: 'success',
                  duration: 2000
                })
                setTimeout(() => {
                  this.$router.push('/get-detail/' + this.vendorId)
                }, 1000)
              }
            })
          } else {
            console.log('error submit!!')
            return false
          }
        })
      }
    },
    mounted () {
      axios.post('/api/vendor/getDetail', {vendorId: this.$route.params.id}).then(res => {
        this.connectInfo = res.data.data
      })
    }
  }
</script>

<style lang="less" rel="stylesheet/less">
  .vendor-connect-person {
    border: 1px solid #aaa;
    padding: 30px;
    margin: 0 auto;
    h3 {
      background: #eee;
      border: 1px solid #aaa;
      margin-bottom: 10px;
      padding: 5px;
    }
    width: 70%;
  }
</style>
